//
//  SNOrderFormDetailAdressCell.m
//  app
//
//  Created by 王少宁 on 15/5/7.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//  订单详情的收货人信息 cell

#import "SNOrderFormDetailAdressCell.h"

@interface SNOrderFormDetailAdressCell ()
@property (nonatomic, retain) UILabel * basicInfoLabel;
@property (nonatomic, retain) UILabel * phoneNumLabel;
@property (nonatomic, retain) UILabel * fullAddressLabel;
@property (nonatomic, retain) UIButton * addressBtn;
@property (nonatomic, retain) UILabel * storehouseLabel;

@property (nonatomic, retain) UIView * expressView;
@property (nonatomic, retain) UIImageView * pointImgView;
@property (nonatomic, retain) UILabel * expressLabel;
@property (nonatomic, retain) UILabel * express_snLabel;
@property (nonatomic, retain) UIImageView * rightImgView;
@property (nonatomic, retain) UIButton * expressBtn;
@property (nonatomic, assign) BOOL includeExpressInfo;
@property (nonatomic, retain) SNOrderFormExpressInfoModel * expressInfoModel;
@end

@implementation SNOrderFormDetailAdressCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)createCell
{
    _basicInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(UF(24), 10, SCREEN_WIDTH - 100, 30)];
    _basicInfoLabel.ufo.origin = CGPointMake(UF(24), 10);
    _basicInfoLabel.textColor = [XCommon hexStringToColor:kColor_l_light_gray_text];
    _basicInfoLabel.font = JKAppGlobleFontMenu;
    [self.contentView addSubview:_basicInfoLabel];
    
    _phoneNumLabel = [[UILabel alloc] init];
    _phoneNumLabel.textAlignment = NSTextAlignmentLeft;
    _phoneNumLabel.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
    _phoneNumLabel.font = JKAppGlobleFontMinTagText;
    [self.contentView addSubview:_phoneNumLabel];
    
    _fullAddressLabel = [[UILabel alloc] init];
    _fullAddressLabel.font = JKAppGlobleFontSmallTagText;
    _fullAddressLabel.textColor = [XCommon hexStringToColor:kColor_m_light_gray_text];
    _fullAddressLabel.numberOfLines = 0;
    [self.contentView addSubview:_fullAddressLabel];
}

- (void)cellWithModel:(SNOrderFormReceiverInfoModel *)model
{
    NSString * basicInfoStr = [NSString stringWithFormat:@"收货人 : %@",model.receiver];
    _basicInfoLabel.text = basicInfoStr;
    [_basicInfoLabel sizeToFit];
    if (_basicInfoLabel.ubo.width < SCREEN_WIDTH - 100) {
        _basicInfoLabel.frame = CGRectMake(UF(24), 0, _basicInfoLabel.ubo.width, 30);
    } else {
        _basicInfoLabel.frame = CGRectMake(UF(24), 0, SCREEN_WIDTH - 100, 30);
    }
    
    _phoneNumLabel.text = model.phone;
    [_phoneNumLabel sizeToFit];
    _phoneNumLabel.ufo.origin = [_basicInfoLabel.ufo fx:1.0 ox:10 fy:1.0 oy:-_phoneNumLabel.ubo.height];
    
    _fullAddressLabel.text = model.fullAddress;
    _fullAddressLabel.frame = CGRectOf([_basicInfoLabel.ufo fx:0 fy:1.0],
                                       [self.contentView.ubo uw:-48 ow:SCREEN_WIDTH oh:[XCommon heightForString:model.fullAddress fontSize:13.0f andWidth:SCREEN_WIDTH - UF(48)] + 10]);
    
    if (model.isStorehouse) {
        [self createStorehouseViewWith:model.storehouse];
    }
    
    self.includeExpressInfo = model.includeExpressInfo;
    if (model.includeExpressInfo) {//有物流信息
        _expressInfoModel = model.expressInfoModel;
        [self createExpressViewWithStorehouseView:model.isStorehouse];
    }
}

- (void)createStorehouseViewWith:(NSString *)storehouse
{
    _storehouseLabel = CREATE_WITH_FRAME_OF(UILabel, [_fullAddressLabel.ufo fx:0 fy:1.0],
                                            [self.ubo uw:-48 ow:SCREEN_WIDTH oh:30]);
    _storehouseLabel.textColor = [XCommon hexStringToColor:kColor_l_light_gray_text];
    _storehouseLabel.font = JKAppGlobleFontMenu;
    _storehouseLabel.text = [NSString stringWithFormat:@"自提地址:%@",storehouse];
    [self.contentView addSubview:_storehouseLabel];
}

- (void)addressBtnClick:(UIButton *)sender withEvent:(UIEvent*)event
{
    UITouch* touch = [[event allTouches] anyObject];
    
    if (touch.tapCount == 2) {
        if ([self.deletage respondsToSelector:@selector(orderFormDetailAdressCellClick)]) {
            [self.deletage performSelector:@selector(orderFormDetailAdressCellClick)];
        }
    }
}

- (void)createExpressViewWithStorehouseView:(BOOL)storehouseView
{
    if (storehouseView) {
        _expressView = [[UIView alloc] initWithFrame:CGRectMake(0, _storehouseLabel.ufo.yy, SCREEN_WIDTH, 70)];
    } else {
        _expressView = [[UIView alloc] initWithFrame:CGRectMake(0, _fullAddressLabel.ufo.yy, SCREEN_WIDTH, 70)];
    }
    
    [self.contentView addSubview:_expressView];

#warning - 因为目前快递信息还不能跳转,因此这个右侧的剪头先隐藏掉了...
    _rightImgView = CREATE_WITH_FRAME_OF(UIImageView, CGPointMake(SCREEN_WIDTH - 25, _expressView.ubo.height*0.5-10),
                                         [self.contentView.ubo ow:10 oh:20]);
//    _rightImgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"arrow_right@2x" ofType:@"png"]];
    [_expressView addSubview:_rightImgView];
    
    _pointImgView = [[UIImageView alloc] initWithFrame:CGRectMake(UF(24), 16, 15, 15)];
    _pointImgView.image = [UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"logisticsPoint_orange@2x" ofType:@"png"]];
    [_expressView addSubview:_pointImgView];
    
    _expressLabel = CREATE_WITH_FRAME_OF(UILabel, [_pointImgView.ufo fx:1.0 ox:5 fy:0 oy:-3],
                                         [self.contentView.ubo ow:200 oh:20]);
    _expressLabel.text = @"快递信息";
    _expressLabel.font = JKAppGlobleFontMenu;
    _expressLabel.textColor = [XCommon hexStringToColor:kColor_l_light_gray_text];
    [_expressView addSubview:_expressLabel];
    
    _express_snLabel = CREATE_WITH_FRAME_OF(UILabel, [_expressLabel.ufo fx:0 fy:1.0 oy:5],
                                            [self.contentView.ubo ow:_rightImgView.ufo.x-_expressLabel.ufo.x oh:20]);
    _express_snLabel.font = JKAppGlobleFontSmallTagText;
    _express_snLabel.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
    _express_snLabel.text = [NSString stringWithFormat:@"%@ %@",self.expressInfoModel.express_company,self.expressInfoModel.express_sn];
    [_expressView addSubview:_express_snLabel];
    
    _expressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _expressBtn.frame = _expressView.bounds;
    [_expressBtn addTarget:self action:@selector(expressBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_expressView addSubview:_expressBtn];
    
    _addressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _addressBtn.frame = _expressView.frame;
    [_addressBtn addTarget:self action:@selector(addressBtnClick:withEvent:) forControlEvents:UIControlEventTouchDownRepeat];
    [self.contentView addSubview:_addressBtn];
}

- (void)expressBtnClick
{
    if ([self.deletage respondsToSelector:@selector(orderFormDetailExpressCellClick)]) {
        [self.deletage performSelector:@selector(orderFormDetailExpressCellClick)];
    }
}

- (void)drawRect:(CGRect)rect
{
    [self drawBackground:rect color:[UIColor whiteColor]];
    if (self.includeExpressInfo) {
        [self drawHorizonLineLeft:UF(24) right:0 at:_expressView.ufo.x - UP(1) color:[XCommon hexStringToColor:kColor_s_separator_cell_line]];
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
